#include "UserAllHead.h"

#if TM1650_EN
static void TM1650_IIC_start(void)
{
	TM1650_IIC_SCL_HIGH; // SCL=1
	TM1650_IIC_SDA_HIGH; // SDA=1
	TM1650_IIC_DELAY_4US;
	TM1650_IIC_SDA_LOW; // SDA=0
	TM1650_IIC_DELAY_4US;
	TM1650_IIC_SCL_LOW; // SCL=0
}

static void TM1650_IIC_stop(void)
{
	TM1650_IIC_SCL_LOW; // SCL=0
	TM1650_IIC_SDA_LOW; // SDA=0
	TM1650_IIC_DELAY_4US;
	TM1650_IIC_SCL_HIGH; // SCL=1
	TM1650_IIC_DELAY_4US;
	TM1650_IIC_SDA_HIGH; // SDA=1
}

static void TM1650_IIC_write_byte(uchar dat)
{
	uchar i;

	TM1650_IIC_SCL_LOW;
	for (i = 0; i < 8; i++)
	{
		TM1650_IIC_SDA_WR(dat & 0x80);
		dat <<= 1;

		TM1650_IIC_DELAY_2US;
		TM1650_IIC_SCL_HIGH;
		TM1650_IIC_DELAY_2US;
		TM1650_IIC_SCL_LOW;
		TM1650_IIC_DELAY_2US;
	}
}

static uchar TM1650_IIC_wait_ack(void)
{
	uchar ack_signal = 0;

	TM1650_IIC_SDA_HIGH; // SDA=1
	TM1650_IIC_DELAY_2US;
	TM1650_IIC_SCL_HIGH;
	TM1650_IIC_DELAY_2US;
	// if(TM1650_IIC_SDA_RD()) ack_signal = 1;   //???????NACK??
	TM1650_IIC_SCL_LOW;
	TM1650_IIC_DELAY_2US;
	return ack_signal;
}
void TM1650_print(uchar dig, uchar seg_data)
{
	TM1650_IIC_start();
	TM1650_IIC_write_byte(dig * 2 + 0x68);
	TM1650_IIC_wait_ack(); //???????0x68
	TM1650_IIC_write_byte(seg_data);
	TM1650_IIC_wait_ack(); //????
	TM1650_IIC_stop();
}

void TM1650_cfg_display(uchar param)
{
	TM1650_IIC_start();
	TM1650_IIC_write_byte(0x48);
	TM1650_IIC_wait_ack(); //????
	TM1650_IIC_write_byte(param);
	TM1650_IIC_wait_ack(); //???
	TM1650_IIC_stop();
}


void Disp1650(void)
{
	uchar dig;
	TM1650_IIC_SDA_HIGH; //
	TM1650_IIC_SCL_HIGH; //
	TM1650_cfg_display(TM1650_BRIGHT8); //
	for (dig = TM1650_DIG1; dig <= TM1650_DIG4; dig++)
	{
		TM1650_print(dig, DisData[dig]); //???????0
	}
}


void Tm1650GpioInit(void)
{

}

#endif //TM1650_EN
